package com.company.model;

import com.company.bean.GoodssalesBean;
import com.company.db.DBProvide;
import com.company.util.MyUtils;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

public class GoodModel {

    /**
     * 获取数据库中的记录显示到下拉列表中
     * @return 数据库记录表
     */
    public List<GoodssalesBean> selectGood(){

        String str1 = "SELECT * FROM goodssales ";
        List<GoodssalesBean> list = MyUtils.getNewInstance(DBProvide.class)
                .query(str1,new BeanListHandler<>(GoodssalesBean.class));
        //查询语句，调用数据库中的query方法，返回结果集
        return list;

    }

    /**
     * 修改商品信息
     *
     * @param goodssalesBean 修改商品表
     * @return
     */
    public boolean UpdateGoods(GoodssalesBean goodssalesBean){
        boolean i = false;
        String strSql = "update goodssales set GOODSID=?,GOODSNAME=?,GOODSPRICE=? where DEPTNO=?";
        int k = MyUtils.getNewInstance(DBProvide.class).execute(strSql,
                goodssalesBean.getGOODSID(),goodssalesBean.getGOODSNAME(),
                goodssalesBean.getGOODSPRICE(),goodssalesBean.getDEPTNO());
        if (k>0){
            i=true;
        }
        return i;

    }


    /**
     * 查询商品信息
     *
     * @param deptno 根据商品编号查询
     * @return
     */
    public GoodssalesBean selectGoodByid(String deptno){
        String strSql = "select * from goodssales where DEPTNO=? ";
        GoodssalesBean goodssalesBean =MyUtils.getNewInstance(DBProvide.class)
            .query(strSql,new BeanHandler<>(GoodssalesBean.class),deptno);

        return goodssalesBean;
    }

}
